define([ "exports","jquery","comm","site_tip","mustache.min","files/flashChecker","dialog","space","folder/main","files/ctrl_policies","files/policies","files/copy_share","files/move","text!/static/page/files.html","text!/static/page/file_item.html","text!/static/page/file_icon_list.html","plugin/jquery-ui.min","plugin/jquery.cookie","css!/static/js/jquery/jquery-select-css-style/css/jquery.stylish-select.css"],
		function(exports,$,comm,Site_tip,Mustache,FlashChecker,Dialog,Space,Folder,Ctrl_policies,Policies,Copy_share,Move,FilesTpl,FileItemTpl,FilesItemMoreIconTpl) {
	//相互依赖模块
	/*if(!Folder)
	{
		Folder = require("folder/main");
	}*/
	var site_tip = new Site_tip();
	//require(['plugin/jquery.ajax.ext'],function(){});


	function Files() {
		this.mi_icon_fun = null;
		this.totalPage = 0;
		this.curPage = 1;
		this.tempPage = 1;
		this.is_load_scroll = false;
		this._listTitle = null;
		this._mainTitle = null;
		this.container = $("#mypage");
		this.show = false;
		this.fserch_show = false;
		//排序字段
		this.sortkey = "filecreate";
		//排序升序，降序
		this.sortdesc = "desc";
	}
	/**
	 * 重新设定新建文件夹名字索引最大数
	 */
	function resetCreateNewFolderNameMax(cb){
		var curFolderDom = $("#folderPath").find("[uuid][haslast]");
		var params={};
		if(curFolderDom.length>0)
		{
			params.file_id = curFolderDom.attr("uuid");
		}
		$.getJSON("/files/get_new_folder_num",params,function(jsonData){
			//console.log(jsonData);
			if(jsonData.success){
				var arr = jsonData.data;
				var max = parseInt(arr.defindMax);
				/*if(max>=0)
				{
					$("#waterfall").attr("defindMax",max+1);
				}
				else
				{
					$("#waterfall").attr("defindMax",-1);
				}*/
				$("#waterfall").attr("defindMax",max);
			}
			if($.isFunction(cb))
			{
				cb(jsonData);
			}
		});
	};
	Files.prototype.initCss = function(flag) {
		//comm.loadCss("/static/js/jquery/jquery-scrollbar/jquery.mCustomScrollbar.css");
		//comm.loadCss("/static/js/jquery/jquery-select-css-style/css/jquery.stylish-select.css");
		//comm.loadCss("/static/js/laydate/skins/wowostar/laydate.css");
		//comm.loadCss("/static/js/jquery/datetimepicker/jquery.datetimepicker.css");
		
		this.checkFlash();
	}
	
	Files.prototype.init = function(flag,data,cb) {
		this.show = true;
		this.all_data = data;
		this.loadCb = cb;
		if(!flag){
			flag = false;
			this.totalPage = $("#totalPage").val();
			this._bindUI();
			this.initSroll();
		}
		if(flag){
			this.checkLoadContainer();
		}
		
	}
	
	Files.prototype.checkFlash = function (){
		var fls= new FlashChecker();
		if(!fls.f){
			site_tip.setContent("请检查是否安装或者开启了最新版本的flash？" +
				"<a href='/helpcenter?active=3' target='_blank' class='outer-tip'>查看帮助</a>"
				,true,false);
			$.cookie("noFlash", "yes");

		}
		else if(fls.v < 11)
		{
			$.cookie("noFlash", "yes");
			site_tip.setContent("您的flash版本过低，需要<a href='http://get.adobe.com/cn/flashplayer' target='_blank' class='outer-tip'>更新</a>",true,false);
		}
		else{
			$.cookie("noFlash", null);
		}
	}
	
	Files.prototype.loadData = function() {
		var that = this;
		if(!that.show){
			return;
		}

		var lastTitle = $("#folderPath").find("span[haslast='true']");
		var puuid = lastTitle.attr("uuid");
		var type = comm.getQueryString("type");

		var params = new Object();
		params.page = that.curPage;
		params.puuid = puuid;
		params.colName = that.sortkey;
		params.sortDesc = that.sortdesc;
		if(type != null){
			params.type = type;
		}
		var all_data = that.all_data;
		//console.log(all_data);
		if(all_data.total==0)
		{
			that.addEmptyView();
		}
		if(all_data && all_data.files)
		{
			$("#totalPage").val(0);
			$("#fileTotal").val(all_data.total);
			$("#main-title span.total").text(all_data.total);
			$("#listPage").val(0);
			that.totalPage = $("#totalPage").val();
			var rdData = {data:all_data['files']};
			if(type==1)
			{
				rdData.type_dowload=1;
			}
			else if(type==2)
			{
				rdData.type_exp = 1;
			}
			else
			{
				rdData.type_common = 1;
			}
			var item_text = Mustache.to_html(FileItemTpl, rdData);
			$("#waterfall").append(item_text);
			if(that.loadCb)
			{
				that.loadCb();
			}
			that._bindUI();
			that.initSroll();

			if($("#wfo").data("detail") == 1){
				var folder = new Folder(that);
				folder.showDetail();
			}
		}
		return false;
	}
	
	/**
	 * 判断html是否存在id为fileList的节点
	 */
	Files.prototype.checkLoadContainer = function() {
		var that = this;
		var len = $("#fileList").length;
		if(len == 0){
			if(!that.show){
				return;
			}
			$("#mainView").prepend(FileItemTpl);
			/*var height = $(window).height() - parseInt($('.wf_header').height()-2);
			var mypageHeight = height - parseInt($('.tool-view').outerHeight(true)) - parseInt($('.main-title').outerHeight(true))-parseInt($('#list-title').outerHeight(true));
			$("#mypage").height(mypageHeight);*/
			that.loaddData.apply(that);
			return false;
		}
		this.loadData();
	}
	
	Files.prototype.initData = function(jsonData) {
		//console.log(jsonData);
		$("#totalPage").val(jsonData.total);
		$("#fileTotal").val(0);
		$("#listPage").val(0);
		this.totalPage = $("#totalPage").val(); 	
		this.fserch_show = true;
		this.show = true;
		this._bindUI();
		this.initSroll();
	}

	Files.prototype.bindBtnDisable = function(btn)
	{
		comm.bindDisableHandler(btn);
	}
	Files.prototype.btnEnable  = function(btn)
	{
		comm.enableBtn(btn);
	}
	Files.prototype._bindUI = function() {
		var that = this;
		var _type = comm.getQueryString("type");
		if(_type==1 || _type==2)
		{
			$(".icon-checkbox").css({"width":0});
		}
        var hideToTopTimer;
        $("#mypage").parent().unbind("scroll").bind("scroll",function(){
            var me = $(this);
            var toTop = $("#listToTop");
            if(hideToTopTimer)
            {
                clearTimeout(hideToTopTimer);
            }
            if(me.scrollTop()>10)
            {
                toTop.stop(true,false).fadeIn();
                hideToTopTimer = setTimeout(function(){
                    toTop.fadeOut();
                },3000);
            }
            else
            {
                toTop.stop(true,false).fadeOut();
            }
            if($("#wfo").data("detail"))
            {
                toTop.addClass("expand");
            }
            else
            {
                toTop.removeClass("expand");
            }

        });
        $("#listToTop").unbind("click").bind("click",function(){
            var me=$(this);
            $("#mypage").parent().animate({"scrollTop":0});
            me.stop(true,false).fadeOut();
        }).unbind("mouseenter").bind("mouseenter",function(){
            clearTimeout(hideToTopTimer);
        }).unbind("mouseleave",function(){
            var me=$(this);
            hideToTopTimer = setTimeout(function(){
                me.fadeOut();
            },5000);
        });

        /*$("#mypage").scroll(function(){
            console.log("scroll.....");
        });*/
		$("#mypage").off("click",".wfc");
		$("#mypage").on("click",".wfc",function(e){
			//e.stopPropagation();
			that.show_iconList(false);
			var thisRow = $(this);
			thisRow.removeClass("item-upSuc");
			/*if(thisRow.hasClass("item-hover"))
			{
				thisRow.removeClass("item-hover");
			}
			else
			{
				thisRow.addClass("item-hover");
			}*/

			//return;
			var itemClick = $(e.target);
			var checkbox = thisRow.find(".icon-checkbox");
			//console.log("hahah");
            //console.log(itemClick);
			//console.log(itemClick.hasClass("icon-locus"));
        	if(!itemClick.hasClass("icon-lock") && !itemClick.hasClass("icon-share") 
        			&& !itemClick.hasClass("icon-del") && !itemClick.hasClass("icon-locus")
        			&& !itemClick.hasClass("icon-more") && !itemClick.hasClass("edit-txt") 
        			&& !itemClick.hasClass("icon-rename") && !itemClick.hasClass("icon-checkbox")){
                that.isRevChk(e,checkbox,thisRow);
                if($("#wfo").data("detail") == 1){
                    var folder = new Folder(that);
                    folder.showDetail();
                }
        		/*var checkbox = $(this).find(".icon-checkbox:visible");
        		that.isRevChk(checkbox);
        		if($("#wfo").data("detail") == 1){
					require(["folder/main"],function(Folder){
						var folder = new Folder();
						folder.showDetail();
					});
				}*/
            }
            else
			{
                /*if(e.shiftKey)
                {
                    console.log("asdfsadf");
                    thisRow.prevAll(function(){
                        console.log($(this));
                    });
                }
                else
                {

                }*/
                checkbox.addClass("hover");
				thisRow.addClass("item-hover");
            }
			/*if(checkbox.hasClass("hover")){
				thisRow.addClass("item-hover");
			}
			else{
				thisRow.removeClass("item-hover");
			}*/
			return false;
		});
		/*$("#mypage").on("click",".icon-outer",function(e){
			return false;
		});*/
		/*$("#mypage").on("click",".icon-checkbox",function(){
			that.isRevChk($(this));
		});*/
		$("#mypage").off("mouseenter",".wfc[data-type='file']");
		$("#mypage").on("mouseenter",".wfc[data-type='file']",function(e){
			if(!isSelectMore($(this))){
				return;
			}
			
			if(that.is_showIconList()){
				return;
			}
			/*if(that.mi_icon_fun != null){
				clearInterval(that.mi_icon_fun);
				that.mi_icon_fun = null;
			}*/
			that.show_mi_icon(true,$(this));
			//that.mi_icon_fun = setTimeout(that._show_mi_icon.apply(that,[true,$(this)]),10);
		});
		
		$("#mypage").off("mouseleave",".wfc[data-type='file']");
		$("#mypage").on("mouseleave",".wfc[data-type='file']",function(e){
			if(that.is_showIconList()){
				return;
			}
			/*if(that.mi_icon_fun != null){
				clearInterval(that.mi_icon_fun);
				that.mi_icon_fun = null;
			}*/
			that.show_mi_icon(false,$(this));
		});
		
		$("#mypage").off("click",".icon-outer");
		$("#mypage").on("click",".icon-outer",function(e){
			var me=$(this);
			if(me.attr("disabled"))
			{
				return false;
			}
			/*$("body").one("ajaxComplete",function(){
				if(typeof(me)!="undefined")
				{
					me.attr("disabled",null);
				}
			});*/
			var action = me.attr("data-action");
			var item = me.parents(".wfc");
			item.find(".icon-checkbox").addClass("hover"); 
			item.addClass("item-hover");
			//me.attr("disabled",true);
			var uuid = item.find("input[name='uuid']").val();
			var name = item.find("div:eq(0)").attr("title");
			//console.log(action);
			switch(action){
				case "policy_control":
					//console.log("访问控制");
					if(item.attr("isforbin") > 0)
					{
						var ctrl_policies = new Ctrl_policies(1,uuid);
						ctrl_policies.item = item;
						ctrl_policies.init("",function(){
							that.policies.apply(that,[uuid,item,me]);
						});
					}
					else
					{
						that.policies.apply(that,[uuid,item,me]);
					}

					
					break;
				case "copy_share":
					//$("#tool-view .icon_share").trigger("click");
					if(item.attr("isforbin") > 0){
						var ctrl_policies = new Ctrl_policies(1,uuid);
						ctrl_policies.item = item;
						ctrl_policies.init("",function()
						{
							var copy_share = new Copy_share();
							var obj = new Object();
							obj.c_uuid = uuid;
							obj.ctype = 1;
							var list = new Array(obj);
							copy_share.showUI(list,name);
						});
						return false;
					}
					else
					{
						var copy_share = new Copy_share();
						copy_share.triggerBtn = me;
						var obj = new Object();
						obj.c_uuid = uuid;
						obj.ctype = 1;
						var list = new Array(obj);
						copy_share.showUI(list,name);
					}

					break;
				case "delete":
					that.remove_item(uuid,item);
					break;
				case "more_tool":
					item.removeClass("item-upSuc");
					item.addClass("item-hover");
					
					if(that.is_showIconList()){
						that.show_iconList(false);
						return;
					}
					that.load_icon_list.apply(that,[item,$(this)]);
					break;
			}
			e.stopPropagation();
		});
		
		/*$(window).resize(function(){
			$("#mypage").mCustomScrollbar("update");
		});*/
		

		

		

		
		$("#tool-view").off("click",".icon_move");
		$("#tool-view").on("click",".icon_move",function(e){
			var selFiles = $("#mypage").find(".wfc[data-type='file']");
			var selChkboxs = selFiles.find(".icon-checkbox.hover:visible");
			if(selChkboxs.length <1){
				site_tip.setContent("请选择文件",false,true);
				return;
			}
			
			var arr = [];
			
			$(selChkboxs).each(function(){
				var item = $(this).parent().parent().parent();
				var input = item.find("input[name='uuid']");
				arr.push(input.val());
			});
			
			
			var del = function (){
				$(selChkboxs).each(function(){
					var item = $(this).parent().parent().parent();
					item.remove();
                    if($("#waterfall").children().size()<=0)
                    {
                        that.addEmptyView();
                    }
				});
			}

			var m = new Move(arr,del);
			m.init();

		});

		$("#tool-view").off("click",".icon_share");
		$("#tool-view").on("click",".icon_share",function(e){
			$(this).blur();
			
			that.forbineds("share");
			return false;
		});
		
		
		/**
		 * 访问控制
		 */

		$("#tool-view").off("click",".icon_control");
		$("#tool-view").on("click",".icon_control",function(e){
			$(this).blur();
			that.forbineds("policies");
			return false;
		});
		
		
		$("#tool-view").on("click",".icon_del",function(e){
			var selFiles = $("#mypage").find(".wfc[data-type='file']");
			var fileChkboxs = selFiles.find(".icon-checkbox.hover:visible");
			var items = new Array();
			var file_uuids = [];
			
			$(fileChkboxs).each(function(){
				var item = $(this).parent().parent().parent();
				var input = item.find("input[name='uuid']");
				file_uuids.push(input.val());
				items.push(item);
				
			});
			
			var selFolders = $("#mypage").find(".wfc[data-type='folder']");
			var folderChkboxs = selFolders.find(".icon-checkbox.hover:visible");
			var folder_uuids = [];
			
			$(folderChkboxs).each(function(){
				var item = $(this).parent().parent().parent();
				var input = item.find("input[name='uuid']");
				folder_uuids.push(input.val());
				items.push(item);
			});
			
			if(fileChkboxs.length < 1 && folderChkboxs.length < 1){
				site_tip.setContent("请选择文件",false,true);
				return;
			}
			
			that.removeBatch(file_uuids, folder_uuids,items);
		});
		

		/**
		 * 全选复选框
		 */
		$("#selAllFiles").on("click",function(){
			var chcbox = $(this).find(".icon-checkbox");
			if(chcbox.hasClass("hover")){
				chcbox.removeClass("hover");
				$(".wfc").removeClass("item-hover");
				$("#mypage").find(".icon-checkbox").removeClass("hover");
				$("#tool-view").find(".icon_move").show();
				return;
			}

			$(".wfc").addClass("item-hover");
			chcbox.addClass("hover");
			$("#mypage").find(".icon-checkbox").addClass("hover");
			
			var folders = $("#mypage").find(".wfc[data-type='folder']");
			var selChkboxs = folders.find(".icon-checkbox.hover:visible");
			if(selChkboxs.length > 0){
				$("#tool-view").find(".icon_move").hide();
			}else{
				$("#tool-view").find(".icon_move").show();
			}
			
			if($("#wfo").data("detail") == 1){
				var folder = new Folder(that);
				folder.showDetail();
			}
			
			return;
			
		});
		
		
		//文件禁止访问开关
		$("#mypage").on("click",".policy-lock",function(e){
			comm.stopEvent(e);
			var slider = $(this).find(".slider");
			var policy_sta = $(this).find(".policy_sta");
			var type = 0;
			if(!policy_sta.hasClass("hover")){
				type = 1;
			}
			
			var item = $(this).parent();
			var uuid = item.find("input[name='uuid']").val();

			var ctrl_policies = new Ctrl_policies(type,uuid,new Folder());
			ctrl_policies.item = item;
			ctrl_policies.init("",function(){
				if(type==1)
				{
					that.policies.apply(that,[uuid,item]);
				}
			});
		});
		
		this.initDrop();
		
	}




	/**
	 * 批量开启访问控制
	 */
	Files.prototype.openBatch = function(file_uuids,folder_uuids,items,fileChkboxs,folderChkboxs,target){
		var that = this;
		var open = function(params,dialog){
			var file_uuids = params.file_uuids;
			var folder_uuids = params.folder_uuids;
			var items = params.items;
			var fileChkboxs = params.fileChkboxs;
			var folderChkboxs = params.folderChkboxs;
			$.post("/files/update_access_denied",{files:file_uuids.join(","),folders:folder_uuids.join(",")},function(jsonData){
				if(jsonData.success){
					$(items).each(function(){
						var item = $(this);
						item.attr("isforbin",0);
						item.removeClass("forbin");
						
						var pLock = item.find(".policy-lock");
						var slider = pLock.find(".slider");
						var policy_sta = pLock.find(".policy_sta");
						policy_sta.addClass("hover");
						slider.animate({left:'26px'});
					});

					/*$("#glable_mask").remove();
					$("#dialog_shade").remove();*/
					if(target == "policies")
					{
						that.setpoliciesBatch(fileChkboxs,folderChkboxs);
						return;
					}else{
						that.shareBatch();
						return;
					}
					
				}else {
					site_tip.setContent(jsonData.message,true,false);
					if(jsonData.error_code==7100 || jsonData.error_code==7001)
					{
						new Folder().reload();
					}
				}
				dialog.close();
			}, "json");
			return false;
		};
		
		var params = new Object();
		params.file_uuids = file_uuids;
		params.folder_uuids = folder_uuids;
		params.items = items;
		params.fileChkboxs = fileChkboxs;
		params.folderChkboxs = folderChkboxs;
		//console.log(items[0].data());
		var len=0;
		//var len = parseInt(file_uuids.length) + parseInt(folder_uuids.length);
		$.each(items,function(){
			var forbin=$(this).attr("isforbin");
			//console.log($(this));
			len +=parseInt($(this).attr("isforbin") || 0);
		});
		var obj = new Object();
		if(target == "policies"){
			obj.title = "访问控制";
		}else{
			obj.title = "分享";
		}
		obj.content = "所选"+len+"项受控文件已经被禁止访问";
		obj.file_id = params;
		obj.btn_text = "允许访问";
		obj.open = true;

		var dialog = new Dialog(obj, open);
		dialog.init();
	}
	
	
	/**
	 * 批量复制链接
	 */
	Files.prototype.shareBatch = function(){
		var list = $("#mypage").find(".wfc");
		var selChkboxs = list.find(".icon-checkbox.hover:visible");
		
		var dataList = new Array();
		var name = null;
		var isType = 1;
		var firstFolderName = '';
		$(selChkboxs).each(function(index,element){
			var item = $(this).parent().parent().parent();
			var uuid = item.find("input[name='uuid']").val();
			var type = 1;
			if(item.attr("data-type") == "folder"){
				type = 2;
				if(isType == 1){
					isType = 2;
					firstFolderName = item.find('.col-1').attr("title");
				}
			}
			
			var obj = new Object();
			obj.c_uuid = uuid;
			obj.ctype = type;
			
			dataList.push(obj);
		});
		
		if(selChkboxs.length == 1){
			name = selChkboxs.parents('.wfc').find('.col-1').attr("title");
		} else {
			if(isType == 1){
				name = selChkboxs.first().parents('.wfc').find('.col-1').attr("title");
			} else {
				name = firstFolderName;
			}
		}

		var copy_share = new Copy_share();
		copy_share.showUI(dataList,name);
	}
	
	
	
	/**
	 * 批量删除
	 */
	Files.prototype.removeBatch = function(file_uuids,folder_uuids,items){
		var that = this;
		var del = function(params,dialog){
			var fe_uuids = params.file_uuids;
			var fr_uuids = params.folder_uuids;
			var elements = params.items;
			$.post("/files/delete",{files:fe_uuids.join(","),folders:fr_uuids.join(",")},function(jsonData){
				if(jsonData.success){
					/*$("#glable_mask").remove();
					$("#dialog_shade").remove();*/
//					site_tip.setContent("删除成功",false,true);
					site_tip.setContent("删除成功",false,true);
					$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
					
					$(elements).each(function(){
						$(this).remove();
					});
					
					$("#list-title").css("box-shadow","none");
					
					var lastTitle = $("#folderPath").find("span[haslast='true']");
					var puuid = lastTitle.attr("uuid");
					resetCreateNewFolderNameMax();
					
					/*
					require(["upload/upload"],function(Upload){
						upload = new Upload();
						upload.resetData();
					});
					*/

					Space.update(jsonData.data);
					
					if($("#waterfall").find(".wfc").size() == 0){
						if (that.tempPage < that.totalPage) {
							that.tempPage = that.tempPage + 1;
							
							that.curPage = 0;
							/*$("#mypage").height($("#fileList .content").height());*/
							$("#list_loading").css("display", "block");
							that.toNextPage.apply(that);
						}
					}
					
					if($("#wfo").data("detail") == 1){
						var folder = new Folder(that);
						folder.showDetail();
					}

					var oldTotal = parseInt($("#fileTotal").val())||0;
					oldTotal =oldTotal-(folder_uuids.length+file_uuids.length);
					oldTotal = oldTotal<=0?0:oldTotal;
					//console.log(oldTotal);
					that.resetCount(oldTotal);

					if($("#waterfall").children().size()<=0)
					{
						that.addEmptyView();
					}
					//site_tip.setContent("删除成功");
				}
				else{
					site_tip.setContent("删除失败");
				}
				dialog.close();
			}, "json");
			return false;
		}
		
		var params = new Object();
		params.file_uuids = file_uuids;
		params.folder_uuids = folder_uuids;
		params.items = items;
		
		var obj = new Object();
		obj.content = "删除后不可恢复";
		obj.file_id = params;

		var dialog = new Dialog(obj, del);
		dialog.init();
	}
	
	
	Files.prototype._unbindUI = function() {
		$("#mypage").off("click");
	}
	
	Files.prototype.show_iconList = function (flag){
		if(!flag){
			$("#fileItem_iconList").remove();
		}
	}
	
	Files.prototype.policies = function (fileId,item,btn){
		//console.log(this);
		var policies = new Policies();
		policies.loadDataByAjax({file_id : fileId});
		return false;
	}
	
	Files.prototype.is_showIconList = function (){
		var fileIcon = $("#fileItem_iconList");
		if (fileIcon.length > 0) {
			//当前所在iconList的父级是否可见
			if (fileIcon.parents(".icon-view:first").is(":visible")) {
				return true;
			} else {
				//如果不可见则移除已存在的iconList
				fileIcon.remove();
			}
		}
		return false;
	}
	
	Files.prototype.load_icon_list = function (item,more){
		var that = this;
		var file_id= item.find("input[name='uuid']").val();
		var file = new Object();
		file.uuid = file_id;
		file.res_id = item.find("input[name='rid']").val();
		file.s_id = $("#s_id").val();
		var item_text = Mustache.to_html(FilesItemMoreIconTpl, file);
		more.append(item_text);
		$("#fileItem_iconList").off().on("click",function(e){
			comm.stopEvent(e);
		});
		//console.log(more.offset());
		var itemY = item.offset().top;
		var mypage = parseInt($("#mypage").parent().height());
		var abHeight = parseInt($('.wf_header').height()) + parseInt($('.tool-view').height()) + 37+mypage;
		//总的位置
		//console.log(abHeight);
		if($("#list-title").is(":visible")){
			abHeight += 37;
		}
		if(itemY<abHeight && itemY>abHeight-80){
			if(comm.lessThenIE(10) && comm.isIE()){
				item.find(".iconList .triangle").hide();
			}else{
				item.find(".iconList .triangle").addClass("triangle-up");
				item.find(".iconList .panel").addClass("panel-up");
			}
		}
		item.find(".iconList").show();
		//console.log(item.find(".iconList").length);
		more.find(".icon-move").click(function (e){
			var arr = [];
			arr.push(file_id);
			var del = function (){
				item.remove();
                if($("#waterfall").children().size()<=0)
                {
                    that.addEmptyView();
                }
			}

			var m = new Move(arr,del);
			m.init();
		});
		
		return false;
	}
	
	Files.prototype._show_mi_icon = function(flag,item) {
		var that = this;
		return function() {
			that.show_mi_icon(flag,item);
		}
	}
	
	Files.prototype.show_mi_icon = function(flag,item) {
		if(flag){
			item.find(".icon-view").css("display","block");
			return;
		}
		item.find(".icon-view").css("display","none");
	}
	
	
	Files.prototype._showListTitle = function() {
		this._listTitle.show();
	}
	
	Files.prototype._showTitleBg = function() {
		$("#folderPath").show();
	}
	
	Files.prototype.initSroll = function() {
		return;
		if(this.is_load_scroll){
			//$("#mypage").mCustomScrollbar("update");
		}else{
			if($("#waterfall").height()<$("#mypage").height()-5){
				var size = $(".wfc").size();
				var c_height = (size+1)*50;
				if(c_height>$("#mypage").height()){
					$("#waterfall").height($("#mypage").height());
				}else{
					$("#waterfall").height(c_height);
				}
			}
			$.adjustFileListView();
			/*$("#mypage").mCustomScrollbar({
				theme:"minimal",
				scrollInertia:200,
				selectorTitle:$("#list-title")
			});*/
			this.is_load_scroll = true;
			this.curPage = 1;
			this.tempPage = 1;
			var that = this;
			/*$("#mypage").off("mScrollbar.end");
			$("#mypage").on("mScrollbar.end",function (e){
				if (that.curPage > that.totalPage) {
//					alert("没有新数据了...");
					$("#list_loading").css("display", "none");
					$("#mypage").height($("#fileList .content").height());
					$("#mypage").mCustomScrollbar("update");
					return;
				}
				that.curPage += 1;
				that.tempPage +=1;
				$("#mypage").height($("#fileList .content").height());
				$("#list_loading").css("display", "block");
				that.toNextPage.apply(that);
			});*/

			/*$("#mypage").off("mScrollbar.start");
			$("#mypage").on("mScrollbar.start",function (e){
				var e = jQuery.Event("fileList.toTop");
				that.container.trigger(e);
			});*/

			
		}
	}
	
	Files.prototype.remove = function() {
		$("#fileList").remove();
		this.curPage = 1;
		this.tempPage = 1;
		this.is_load_scroll = false;
		this.show = false;
		$("#selAllFiles").off("click");
		$("#tool-view").off();
		
	}
	
	Files.prototype.update = function() {
		if($("#waterfall").height()<$("#mypage").height()-5){
			var size = $(".wfc").size();
			var c_height = (size+1)*50;
			if(c_height>$("#mypage").height()){
				$("#waterfall").height($("#mypage").height());
			}else{
				$("#waterfall").height(c_height);
			}
		}
		if($("#waterfall").height()>=$("#mypage").height()){
			$("#waterfall").height("100%");
		}
		//$("#mypage").mCustomScrollbar("update");
	}
	
	Files.prototype.toNextPage = function() {
		if (this.curPage <= this.totalPage) {
			var that = this;
			if(!that.show){
				return;
			}
			if($("#list-title").css("display") == "none"){
				var keyValue = $("#search").find(".content").val();
				$.getJSON("/f_search",{keyword:keyValue,page:that.curPage},  function(jsonData) {
					if(!that.fserch_show){
						return true;
					}
					if(jsonData.ret == -1){
						$("#list_loading").css("display", "none");
						var lastIndex = $("#waterfall").find(".col-1").last().parent().index();
						var type = comm.getQueryString("type");
						if(type==1)
						{
							jsonData.type_dowload=1;
						}
						else if(type==2)
						{
							jsonData.type_exp = 1;
						}
						else
						{
							jsonData.type_common = 1;
						}
						var item_text = Mustache.to_html(FileItemTpl, jsonData);
						$("#waterfall").append(item_text);
						$("#mypage").height($("#fileList .content").height());
						//$("#mypage").mCustomScrollbar("update");

						var list = $("#waterfall").find(".col-1");
						list.each(function(index,elemen){
							if(index>lastIndex){
								comm.highlight( $(elemen).html(),jsonData.keyword,$(elemen));
							}

						});
					}


				});
				return false;
			}
			var type = comm.getQueryString("type");
			if(type == null){
				type = 0;
			}

			var lastTitle = $("#folderPath").find("span[haslast='true']");
			var puuid = lastTitle.attr("uuid");

			$.getJSON("/files/get_all",{page:that.curPage,puuid:puuid,type:type}, function(jsonData) {
				if(!that.show){
					return;
				}
				if(jsonData.ret == -1){
					$("#list_loading").css("display", "none");
					var item_text = Mustache.to_html(tmpl, jsonData);
					$("#waterfall").append(item_text);
					$("#mypage").height($("#fileList .content").height());
					//$("#mypage").mCustomScrollbar("update");

					$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
				}


			});
			return false;

		}

		if (this.curPage > this.totalPage) {
			$("#list_loading").css("display", "none");
			$("#mypage").height($("#fileList .content").height());
			//$("#mypage").mCustomScrollbar("update");
//			alert("没有新数据了...");

		}
	}
	Files.prototype.resetCount=function(count)
	{
		count = parseInt(count);
		if(count<0 || !count)
		{
			count=0;
		}
		$("#current-rows-count .total").text(count);
		$("#fileTotal").val(count);
		$("#cur_folder_count").text(count);
	}
	//数据为空时的界面添加处理
	Files.prototype.addEmptyView = function()
	{

		$("#list-title").hide();
		$("#mypage").addClass("empty");
		var tpl = $("#file-list-empty-msg-tpl").html();
		$("#mypage").append(tpl);
		var type = comm.getQueryString("type");
		if(!type)
		{
			$.bindUploadFiles("file-empty-upload-btn");
		}
		else
		{
			if(type==1)
			{
				$("#mypage").addClass("download");
			}
			else if(type==2)
			{
				$("#mypage").addClass("exp");
			}
			$("#mypage").find("#upload_form").hide();
		}
	};
	//数据为空时的界面添加处理
	Files.prototype.removeEmptyView = function()
	{
		$("#list-title").show();
		$("#mypage").removeClass("empty").removeClass("empty2");
		$("#mypage").find(".file-list-empty-msg").remove();
	};
	Files.prototype.add_item = function(file) {

		var wfcArr = $("#waterfall").find(".wfc");
		var firstIndex = parseInt(wfcArr.first().attr("f-index"));	
		var lastIndex = parseInt(wfcArr.last().attr("f-index"));	
		var index = 0;
		if(firstIndex > lastIndex){
			index = firstIndex + 1;
		}else{
			if(isNaN(lastIndex)){
				lastIndex = 0;
			}
			index = lastIndex + 1;
		}
		var view = {"data":[{
			"index" : index,
			"type" : file.ext_name,
			"name" : file.name+"."+file.ext_name,
			"uuid" : file.id,
			"size" : get_file_size(file.size),
			"created_ts":comm.formatDate(file.creation_ts*1000),
			"rid" :file.id,
			"id":file.id,
			"access_denied":parseInt(file.access_denied)
		}]};
		/*var type = comm.getQueryString("type");
		if(type==1)
		{
			view.type_dowload=1;
		}
		else if(type==2)
		{
			view.type_exp = 1;
		}
		else
		{
			view.type_common = 1;
		}*/
		/*if(file.access_denied == 1)
		{
			var obj = view.data[0];
			obj.access_denied = file.access_denied;
		}*/
		var type = comm.getQueryString("type");
		if(type==1)
		{
			view.type_dowload=1;
		}
		else if(type==2)
		{
			view.type_exp = 1;
		}
		else
		{
			view.type_common = 1;
		}
		var that = this;
		that.removeEmptyView();
		var item_text = Mustache.to_html(FileItemTpl, view);
		$("#waterfall").prepend(item_text);
		$("#wfc_"+index).addClass("item-upSuc");

		that.initDrop();

		/*if($("#wfo").data("detail") == 1){
			var folder = new Folder(that);
			folder.showDetail();
		}*/

		that.update();
		var oldTotal = parseInt($("#fileTotal").val())||0;
		oldTotal ++;
		that.resetCount(oldTotal);
		return false;
	};
	
	Files.prototype.remove_item = function(file_id,item) {
		var that = this;
		var del_file = function(file_id){
			$.post("/files/delete",{files:file_id},function(jsonData){
				if(jsonData.success){
					$("#glable_mask").remove();
					$("#dialog_shade").remove();
					site_tip.setContent("删除成功",false,true);
					item.remove();
					that.update();
					var e = jQuery.Event("fileList.removeItem");
					that.container.trigger(e,[jsonData.data]);
					
					var size = $("#mypage").find(".wfc").size();
					if(size == 0){
						$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
					}
					
					if($("#wfo").data("detail") == 1){
						var folder = new Folder(that);
						folder.showDetail();
					}
					var oldTotal = parseInt($("#fileTotal").val())||1;
					oldTotal--;
					that.resetCount(oldTotal);
					if($("#waterfall").children().size()<=0)
					{
						that.addEmptyView();
					}
					Space.update(jsonData.data);
					/*require(["space"],function(space){
						space.update(jsonData.data);
					});*/
				}
				else{
					site_tip.setContent("删除失败");
				}
			},'json');
			return false;
		};
		var obj = new Object();
		obj.content = "文件删除后不可恢复";
		obj.file_id = file_id;

		var dialog = new Dialog(obj, del_file);
		dialog.init();
	};

	Files.prototype.setPoliciesBatch = function(file_uuids,folder_uuids)
	{
		//console.log(file_uuids);
		//console.log(folder_uuids);
		var policies = new Policies();
		var params = {};
		if(file_uuids.length<=0 && folder_uuids.length==1)
		{
			policies.loadDataByAjax({file_id:folder_uuids[0],type:1});
		}
		else if(file_uuids.length==1 && folder_uuids.length<=0)
		{
			policies.loadDataByAjax({file_id:file_uuids[0],type:0});
		}
		else
		{
			params.isBatch = true;
			params.fileUuids = file_uuids||[];
			params.folderUuids = folder_uuids||[];
			policies.batchParam = params;
			policies.init();
		}

	}

	/**
	 * 设置批量访问控制
	 */
	Files.prototype.setpoliciesBatch = function(fileChkboxs,folderChkboxs){
		var that = this;
		var uuid,item;
		//获取文件列表
		var file_uuids = [];
		
		$(fileChkboxs).each(function(){
			item = $(this).parent().parent().parent();
			input = item.find("input[name='uuid']");
			uuid = input.val();
			file_uuids.push(uuid);
		});
		
		
		//获取文件夹列表
		var folder_uuids = [];
		
		$(folderChkboxs).each(function(){
			item = $(this).parent().parent().parent();
			var input = item.find("input[name='uuid']");
			uuid = input.val();
			folder_uuids.push(uuid);
		});
		
		var isBatch = false;
		var fLen = fileChkboxs.length;
		var fdLen = folderChkboxs.length;
		
		if(parseInt(fLen) + parseInt(fdLen) != 1){
			isBatch = true;
		}

		if(isBatch){
			var policies = new Policies();

			var params = new Object();
			params.isBatch = true;
			params.fileUuids = file_uuids;
			params.folderUuids = folder_uuids;
			policies.batchParam = params;
			policies.init();
		}else{
			if(fLen == 1){
				//console.log("heihei");
				that.policies(uuid, item);
				return;
			}
			var folder = new Folder(that);
			folder.policies(uuid, item);
			return;
		}
	};
	
	Files.prototype.listToTop = function (){
		var mypage = $("#mypage");
		if(mypage.length>0){
			//$("#mypage").mCustomScrollbar("scrollTo", 0);
			$("#mypage").scrollTop(0)
		}
	};
	
	//Files.prototype.toggleClass()
	//判断复选框是否选中，
	Files.prototype.isRevChk = function (e,checkbox,row){
       if(!e.ctrlKey && !e.shiftKey)
       {
           $("#mypage").find(".item-hover").not(row).each(function(){
               $(this).removeClass("item-hover").children().children().children(".icon-checkbox").removeClass("hover");
           });
       }
        var ev = jQuery.Event("click");
        ev.shiftKey = e.shiftKey;
       checkbox.trigger(ev,[true]);
       // console.log("ssss");
		//var list;
		/*if(type == undefined){
			list = $("#mypage").find(".wfc");
		}else if(type == 0){
			list = $("#mypage").find(".wfc[data-type='file']");
		}else{
			list = $("#mypage").find(".wfc[data-type='folder']");
		}*/
        //row.toggleClass("item-hover");
        //checkbox.toggleClass("hover");
        /*if(clearOther)
        {
            $("#mypage").find(".item-hover").not(row).each(function(){
                $(this).removeClass("item-hover").children().children().children(".icon-checkbox").removeClass("hover");
            });
            $("#selAllFiles").find(".icon-checkbox").removeClass("hover");
        }
        else
        {
            //checkbox.toggleClass("hover");
            if($("#mypage").find(".icon-checkbox.hover").size()>=$("#mypage").find(".icon-checkbox").size())
            {
                $("#selAllFiles").find(".icon-checkbox").addClass("hover");
            }
            else
            {
                $("#selAllFiles").find(".icon-checkbox").removeClass("hover");
            }
        }*/



        return false;
		/*var selChkboxs = list.find(".icon-checkbox.hover");
		
		
		//选中文件夹的时候，不显示移动icon
		var target = checkbox.parent().parent().parent();
		if(target.attr("data-type") == "folder"){
			$("#tool-view").find(".icon_move").hide();
		}else{
			$("#tool-view").find(".icon_move").show();
		}
		
		var tee = checkbox.parent().parent();
		
		if(selChkboxs.length == 0){
			checkbox.toggleClass("hover");
			if(!checkbox.hasClass("hover")){
				$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
				$(this).removeClass("item-hover");
				return;
			}
			return;
		}
		
		if(selChkboxs.length == 1){
			var item = checkbox.parent().parent().parent();
			var sitem = selChkboxs.parent().parent().parent();
			if(item.attr("id") == sitem.attr("id")){
				checkbox.toggleClass("hover");
				if(!checkbox.hasClass("hover")){
					$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
					$(this).removeClass("item-hover");
					return;
				}
				return;
			}
		}
		
		
		
		$(selChkboxs).each(function(){
			$(this).removeClass("hover");
			var item = $(this).parent().parent().parent();
			item.removeClass("item-hover");
		});
		checkbox.addClass("hover");
		$("#selAllFiles").find(".icon-checkbox").removeClass("hover");*/
	};
	function accDiv(arg1,arg2){
		var t1=0,t2=0,r1,r2;
		try{t1=arg1.toString().split(".")[1].length}catch(e){}
		try{t2=arg2.toString().split(".")[1].length}catch(e){}
		with(Math){
			r1=Number(arg1.toString().replace(".",""))
			r2=Number(arg2.toString().replace(".",""))
			return (r1/r2)*pow(10,t2-t1);
		}
	}
	function get_file_size (byte) {
		//小于1K
		var unit="",b="";
		if(byte<1024)
		{
			b = byte;
			unit = "B";
		}
		//小于1MB
		else if (byte < 1048576)
		{
			b = round_dp(accDiv(byte,1024),2);
			unit = "KB";
		}
		else if (byte < 1073741824)
		{
			b = round_dp(accDiv(byte,1048576),2);
			unit = "MB";
		}
		else if(byte < 1099511627776)
		{
			b = round_dp(accDiv(byte,1073741824),2);
			unit = "GB";
		}
		else
		{
			b = round_dp(accDiv(byte,1099511627776),2);
			unit = "TB";
		}
		b += unit;
		return b;
	}
	
	function round_dp($num, $dp) {
		$sh = Math.pow(10, $dp);
		//console.log(Math.round($num * $sh));
		return Math.round($num * $sh) / $sh;
	}
	
	
	function isSelectMore(item){
		var selFiles = $("#mypage").find(".wfc");
		var selChkboxs = selFiles.find(".icon-checkbox.hover:visible");
		if(selChkboxs.length > 1){
			return false;
		}
		if(selChkboxs.length == 1){
			var chkItem = selChkboxs.parent().parent().parent();
			var chkInput = chkItem.find("input[name='uuid']");
			var selInput = item.find("input[name='uuid']");
			if(chkInput.val() == selInput.val()){
				return true;
			}
			return false;
		}
		return true;
	}
	
	
	function createDrop(){
		var selFiles = $("#mypage").find(".wfc[data-type='file']");
		var selChkboxs = selFiles.find(".icon-checkbox.hover:visible");
		var num = selChkboxs.size();
		if(num == 0){
			num = 1;
		}
		if(num > 99){
			num = "N"
		}
		var html = '<div class="drap_tip">'
			+'<div class="type"><span class="num">'+num+'</span></div>'
			+'</div>';
		return html;
	}
	
	/**
	 * 遍历出已禁止文件
	 */
	Files.prototype.forbineds = function (target,btn){
		var that=this;
		//console.log("asdfasdfasdfasdf");
		var selFiles = $("#mypage").find(".wfc[data-type='file']");
		var fileChkboxs = selFiles.find(".icon-checkbox.hover:visible");
		var file_uuids = [];
		var items = new Array();
		//var forbiddenLen = 0;
		var f_ids=[];
		$(fileChkboxs).each(function(){
			var item = $(this).parent().parent().parent();
			var input = item.find("input[name='uuid']");
			if(item.attr("isforbin") > 0){
				file_uuids.push(input.val());
				items.push(item);
				//forbiddenLen +=
			}
			f_ids.push(input.val());
		});

		
		var selFolders = $("#mypage").find(".wfc[data-type='folder']");
		var folderChkboxs = selFolders.find(".icon-checkbox.hover:visible");
		var folder_uuids = [];
		var fd_ids = [];
		$(folderChkboxs).each(function(){
			var item = $(this).parent().parent().parent();
			//console.log(item.data());
			var input = item.find("input[name='uuid']");
			if(item.attr("isforbin") > 0 && target != "share"){
				folder_uuids.push(input.val());
				items.push(item);
			}
			fd_ids.push(input.val());
		});
		if(f_ids.length<=0 && fd_ids.length<=0)
		{
			site_tip.setContent("请选择文件");
			return false;
		}
		//如果没有选中文件夹，而且有禁用的文件
		if(file_uuids.length>0 && folderChkboxs.length<=0)
		{
			//new Ctrl_policies().openBatch();
			//that.openBatch(file_uuids, folder_uuids, items,fileChkboxs,folderChkboxs,target);
			var ctrl_policies = new Ctrl_policies(1,'',new Folder());
			var files = file_uuids;
			var folders = [];
			var text = files.length>0?"所选项中包含"+files.length+"个禁用访问的文件，请先开启允许访问":null;
			ctrl_policies.openBatch(files,folders,function(){
				that.setPoliciesBatch(f_ids,fd_ids);
			},text);
		}
		//没有选中文件夹，而且没有禁用文件
		else if(file_uuids.length<=0 && folderChkboxs.length<=0)
		{
			if(file_uuids.length == 0 && folderChkboxs.length == 0 && target == "policies"){
				that.setPoliciesBatch(f_ids,fd_ids);
			}
			//console.log(target);
			if(file_uuids.length == 0 && folderChkboxs.length == 0 && target == "share")
			{
				that.shareBatch();
			}
		}
		else if(target == "policies")
		{
			new Policies().loadDataByAjax({files:f_ids,folders:fd_ids},"/policies/check_access_denied",true,function(ret,win){
				win.remove();
				if(ret.files||ret.folders)
				{
					var ctrl_policies = new Ctrl_policies(1,'',new Folder());
					var files = ret.files||[];
					var folders = ret.folders||[];
					var text = files.length>0?"所选项中包含"+files.length+"个禁用访问的文件，请先开启允许访问":null;
					ctrl_policies.openBatch(files,folders,function(){
						that.setPoliciesBatch(f_ids,fd_ids);
					},text);
				}
				else
				{
					that.setPoliciesBatch(f_ids,fd_ids);
				}
			});
			/*$.post("/policies/check_access_denied",{files:f_ids,folders:fd_ids},function(ret){
				if(ret.files||ret.folders)
				{
					var ctrl_policies = new Ctrl_policies(1,'',new Folder());
					var files = ret.files||[];
					var folders = ret.folders||[];
					var text = files.length>0?"所选项中包含"+files.length+"个禁用访问的文件，请先开启允许访问":null;
					ctrl_policies.openBatch(files,folders,function(){
						that.setPoliciesBatch(f_ids,fd_ids);
					},text);
				}
				else
				{
					that.setPoliciesBatch(f_ids,fd_ids);
				}
			},'json');*/
		}
		else
		{
			this.shareBatch();
		}
		/*$(folderChkboxs).each(function(){
			var item = $(this).parent().parent().parent();
			//console.log(item.data());
			if(item.attr("isforbin") > 0 && target != "share"){
				var input = item.find("input[name='uuid']");
				folder_uuids.push(input.val());
				items.push(item);
			}
		});

		if(fileChkboxs.length < 1 && folderChkboxs.length < 1){
			site_tip.setContent("请选择文件",false,true);
			return false;
		}*/
		//没有禁用的文件以及文件夹下面有禁用的文件
		/*if(file_uuids.length == 0 && folderChkboxs.length == 0 && target == "policies"){
			this.setpoliciesBatch(fileChkboxs,folderChkboxs);
			return true;
		}
		//console.log(target);
		if(file_uuids.length == 0 && folderChkboxs.length == 0 && target == "share")
		{
			this.shareBatch();
			return true;
		}*/

		//this.openBatch(file_uuids, folder_uuids, items,fileChkboxs,folderChkboxs,target);
		
		return true;
	}
	
	
	Files.prototype.initDrop = function (){
        var that = this;
		$(".wfc[data-type='file']").draggable({
			//addClasses:true,
			helper: createDrop,
			cursorAt: { right:35,bottom:35 },
			zIndex: 100,
			delay: 300,
			//revert: true,
			start: function( event, ui ) {
				//console.log(event);
				//$("#waterfall").parent().css("position","static");
				//$("#waterfall").parent().parent().css("position","static");
				var file = $(event.target);
				var chk = file.find(".icon-checkbox:visible");
				chk.addClass("hover");
			},
			stop: function( event, ui ) {
				//$("#waterfall").parent().css("position","relative");
				//$("#waterfall").parent().parent().css("position","relative");
			}
		}); 
		
		$(".wfc[data-type='folder']").droppable({
		  hoverClass:"drop-hover",
		  drop: function( event, ui ) {
			var aa = event;
			var folder = $(event.target);
			var uuid = folder.find("input[name='uuid']").val();
			var bb = ui;
			var dd = ui.draggable;
			
			
			var selFiles = $("#mypage").find(".wfc[data-type='file']");
			var selChkboxs = selFiles.find(".icon-checkbox.hover:visible");
			
			var arr = [];
			
			$(selChkboxs).each(function(){
				var item = $(this).parent().parent().parent();
				var input = item.find("input[name='uuid']");
				arr.push(input.val());
			});
			
			
			var del = function (){
				$(selChkboxs).each(function(){
					var item = $(this).parent().parent().parent();
					item.remove();
                    if($("#waterfall").children().size()<=0)
                    {
                        that.addEmptyView();
                    }
				});
			};
			site_tip.loadingMsg("正在移动文件...");
			$.post("/files/move",{files:arr.join(","),folder_id:uuid},function(jsonData){
				if(jsonData.success){
					del();
					var len = $("#mypage").find(".icon-checkbox:visible").size();
					var selLen = $("#mypage").find(".icon-checkbox.hover:visible").size();
					
					if(selLen>0 && len == selLen){
						$("#selAllFiles").find(".icon-checkbox").addClass("hover");
					}else{
						$("#selAllFiles").find(".icon-checkbox").removeClass("hover");
					}
					
					site_tip.setContent("移动文件成功",false,true);
				}
				else
				{
					site_tip.setContent(jsonData.message,false,true);
				}
			}, "json");
			
			return false;
			
			
		  }
		});
	}

	Files.prototype.reload = function(){
		var curFolderDom = $("#folderPath").find("[uuid][haslast]");
		var curID=curFolderDom.attr("uuid")||0;
		new Folder().get_all(curID);
	};

	return Files;

});